<template>
  <div class="mod-admin-groupProd">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <template slot-scope="scope" slot="menuLeft">
        <el-button
          icon="el-icon-arrow-left"
          type="primary"
          size="small"
          @click="goBackHandle()"
          >{{ $t("groups.returnToJoinGroupActivities") }}</el-button
        >
        <el-button
          :disabled="
            groupActivity.activityStatus != 1 &&
            groupActivity.activityStatus != 5
          "
          icon="el-icon-plus"
          type="primary"
          size="small"
          @click="selectProdHandle()"
          >{{ $t("groups.newEventProduct") }}</el-button
        >
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-edit"
          size="small"
          type="text"
          @click="editSkuhandle(scope.row.groupProdId)"
          >{{ $t("groups.activitySpecifications") }}</el-button
        >
        <el-button
          icon="el-icon-folder-delete"
          size="small"
          type="text"
          :disabled="
            scope.row.activityStatus !== 2 || scope.row.groupProdStatus === 2
          "
          @click="invalidGroupProdHandle(scope.row)"
          >{{ $t("groups.expiredGoods")
          }}<el-tooltip
            class="item"
            effect="dark"
            :content="$t('groups.failureTip')"
            placement="right"
          >
            <i class="el-icon-question"></i>
          </el-tooltip>
        </el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="text"
          style="color: red"
          :disabled="
            (scope.row.activityStatus === 2 || scope.row.activityStatus > 5) &&
            scope.row.groupProdStatus === 1
          "
          @click="deleteHandle(scope.row)"
          >{{ $t("text.delBtn") }}</el-button
        >
      </template>
      <template slot-scope="scope" slot="pic">
        <img :src="scope.row.pic" width="100" height="100" />
      </template>
      <template slot-scope="scope" slot="groupProdStatus">
        <el-tag
          type="info"
          v-if="scope.row.groupProdStatus === 1"
          size="mini"
          >{{ $t("publics.normal") }}</el-tag
        >
        <el-tag
          type="warning"
          v-if="scope.row.groupProdStatus === 2"
          size="mini"
          >{{ $t("groups.expired") }}</el-tag
        >
      </template>
    </avue-crud>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="refreshChange"
    ></add-or-update>

    <!-- 商品选择弹窗-->
    <!-- 如果要更改dataUrl，在商品选择组件选择商品后的判断处也要更改一下 -->
    <prods-select
      v-if="selectGroupProdVisible"
      ref="ProdsSelect"
      dataUrl="/group/prod/getNotGroupProdPage"
      @refreshSelectProds="selectGroupProds"
    ></prods-select>
    <!-- 活动商品规格属性设置 -->
    <GroupProdSetting v-if="groupProdSettingVisible" ref="GroupProdSetting">
    </GroupProdSetting>
  </div>
</template>

<script>
import GroupProdSetting from './groupProdSkuSetting'
import ProdsSelect from '@/components/prods-select'
import { tableOption } from '@/crud/group/groupProd'
import AddOrUpdate from './groupProd-add-or-update'

export default {
  data () {
    return {
      dataList: [],
      groupActivity: {},
      selectGroupProdVisible: false,
      groupProdSettingVisible: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    ProdsSelect,
    GroupProdSetting
  },
  created () {

  },
  mounted () {
  },
  methods: {
    getDataList (page, params) {
      this.dataListLoading = true
      this.getActivityInfo()
      this.$http({
        url: this.$http.adornUrl('/group/prod/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize,
              groupActivityId: this.$route.query.activityId
            },
            params
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 获取活动设置
    getActivityInfo () {
      if (!this.dataList.records && this.$route.query.activityId) {
        this.$http({
          url: this.$http.adornUrl('/group/activity/info/' + this.$route.query.activityId),
          method: 'GET',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.groupActivity = data
        })
      }
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    deleteHandle (row) {
      this.$confirm(this.$i18n.t('groups.isSuer') + row.prodName + this.$i18n.t('groups.isDele'), this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/group/prod/' + row.groupProdId),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    /**
     * 刷新回调
     */
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.getDataList(this.page, params)
    },
    // 选择商品操作
    selectProdHandle () {
      this.selectGroupProdVisible = true
      this.$nextTick(() => {
        this.$refs.ProdsSelect.init()
      })
    },
    // 选择商品回调
    selectGroupProds (prods) {
      if (prods) {
        // 获取所有的商品
        let prodIds = []
        for (let index = 0; index < prods.length; index++) {
          prodIds.push(prods[index].prodId)
        }
        this.saveGroupProds(prodIds)
      }
    },
    // 保存活动商品
    saveGroupProds (prodIds) {
      this.$http({
        url: this.$http.adornUrl('/group/prod/saveByProdIds'),
        method: 'post',
        data: this.$http.adornData({
          'groupActivityId': this.$route.query.activityId,
          'prodIds': prodIds
        })
      }).then(({ data }) => {
        this.$message({
          message: this.$i18n.t('publics.operation'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            this.getDataList(this.page)
          }
        })
      })
    },
    /**
     * 查询商品是否在参与秒杀活动
     */
    prodIsSeckill (prodIds) {
      this.$http({
        url: this.$http.adornUrl('/admin/discount/prodIsSeckill'),
        method: 'GET',
        data: this.$http.adornData({
          'prodIds': prodIds
        })
      }).then(({ data }) => {
        return data
      })
    },
    // 编辑活动sku
    editSkuhandle (groupProdId) {
      this.groupProdSettingVisible = true
      this.$nextTick(() => {
        this.$refs.GroupProdSetting.init(groupProdId)
      })
    },
    // 返回路由跳转
    goBackHandle () {
      this.$router.go(-1)
    },
    // 失效活动商品
    invalidGroupProdHandle (row) {
      this.$confirm(this.$t('groups.invalidatePrductPre') + row.prodName + this.$t('groups.invalidatePrductSuf'), this.$t('text.tips'), {
        confirmButtonText: this.$t('crud.filter.submitBtn'),
        cancelButtonText: this.$t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(
        () => {
          this.$http({
            url: this.$http.adornUrl('/group/prod/invalid/' + row.groupProdId),
            method: 'PUT',
            data: this.$http.adornData({})
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        }).catch(() => { })
    }
  }
}
</script>
<style lang="scss">
.mod-admin-groupProd {
}
</style>
